﻿namespace ScrumLite.Services.Tests
{
    using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Security.Authentication;
    using System.Security.Principal;
    using System;
    using System.Diagnostics;
    using System.Collections.ObjectModel;
    using ScrumLite.Services.Contracts;

    /// <summary>
    /// Summary description for ActiveDirectoryProfileServiceFixture
    /// </summary>
    [TestClass]
    public class ActiveDirectoryProfileServiceFixture
    {
        [TestMethod]
        public void ShouldReturnUserNameOtherThanAnonymous()
        {
            ActiveDirectoryProfileService service = new ActiveDirectoryProfileService();
            string fullName = service.GetFullName("Administrator");

            // There are no builtin accounts with FullName set so this should be null
            // The test pass if no exception is thrown
            Assert.IsNull(fullName);
        }

        [TestMethod]
        public void ShouldReturnAnonymousIfUserNotFound()
        {
            ActiveDirectoryProfileService service = new ActiveDirectoryProfileService();
            string fullName = service.GetFullName("NotExists");

            Assert.AreEqual("Anonymous", fullName);
        }        

        [TestMethod]
        public void ShouldReturnUserInRole()
        {
            ActiveDirectoryProfileService service = new ActiveDirectoryProfileService();
            bool isInRole = service.IsUserInRole("Administrator", "Administrators");
            
            Assert.IsTrue(isInRole);
        }

        [TestMethod]
        [ExpectedException(typeof(AuthenticationException))]
        public void ShouldThrowForNonExistingUserInRole()
        {
            ActiveDirectoryProfileService service = new ActiveDirectoryProfileService();
            bool isInRole = service.IsUserInRole("Nuevo", "Test");
            Assert.IsFalse(isInRole);
         }

        [TestMethod]
        [ExpectedException(typeof(AuthenticationException))]
        public void ShouldThrowForNonExistingUserOnGettingRoles()
        {
            ActiveDirectoryProfileService service = new ActiveDirectoryProfileService();
            string[] roles = service.GetUserRoles("NonExistingUser");
            Assert.IsNull(roles);
        }

        [TestMethod]
        public void ShouldReturnUsersRoles()
        {
            ActiveDirectoryProfileService service = new ActiveDirectoryProfileService();
            string userId = "Guest";
 
            string[] roles = service.GetUserRoles(userId);

            Assert.IsNotNull(roles);
            Assert.IsTrue(roles.Length > 0);
         }

        [TestMethod]        
        public void ShouldRetrieveAtLeastOneUserFromAD()
        {
            ActiveDirectoryProfileService service = new ActiveDirectoryProfileService();
            Collection<Person> people = service.GetPeople();

            Assert.IsTrue(people.Count > 0);
        }
    }
}
